package net.asfun.jvalog.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.domain.Counter;
import net.asfun.jvalog.domain.Dbcache;
import net.asfun.jvalog.domain.Gmcache;
import net.asfun.jvalog.misc.Filter;
import net.asfun.jvalog.misc.Setting;
import net.asfun.jvalog.service.Painter;
import net.asfun.jvalog.view.SiteInfo;

public class CacheAction extends BaseAction{
	
	public void index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		this.assign("memoryStat", Filter.breakline(Gmcache.stat()));
		this.assign("dbstoreStat", Filter.breakline(Dbcache.stat()));
		this.render("admin/caches.html", resp);
	}
	
	public void clear(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Setting.reset();
		SiteInfo.reset();
		Painter.reset();
		Counter.rebuild();
		Gmcache.clear();
		Dbcache.clear();
		this.feedback("cache.clear", req, resp);
	}
	
	public void dbclear(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Dbcache.clear();
		this.feedback("cache.dbclear", req, resp);
	}
	
	public void gmclear(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Gmcache.clear();
		this.feedback("cache.gmclear", req, resp);
	}
	
	//TODO 删除各自使用不经常的cache条目，尽量保证缓存页面只在一个地方存在copy
	//TODO detete entry last updated over m days & hits less than n times both in gmcache or dbcache.
}
